package com.ygxsk.carhome.views.picker;

import android.content.Context;
import android.util.AttributeSet;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 日期选择
 * Created by ycuwq on 17-12-28.
 */
public class HourPicker extends WheelPicker<Integer> {

    private OnHourSelectedListener mOnHourSelectedListener;

    private int mMinHour, mMaxHour;

    //该小时滚轮选择器之中选中的小时；
    private int mSelectedHour;


    public HourPicker(Context context) {
        this(context, null);
    }

    public HourPicker(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HourPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setItemMaximumWidthText("00");
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMinimumIntegerDigits(2);
        setDataFormat(numberFormat);

        mMinHour = 1;
        mMaxHour = 24;

        updateHour();

        //默认选中的那个小时是：
        // TODO: 2023-07-20 08:05 默认选中的时间是哪个，当前时间还是每天的0点？
        mSelectedHour = 0;//

        setSelectedHour(mSelectedHour, false);

        setOnWheelChangeListener(new OnWheelChangeListener<Integer>() {
            @Override
            public void onWheelSelected(Integer item, int position, String itemString) {
                if (mOnHourSelectedListener != null) {
                    mOnHourSelectedListener.onHourSelected(item);
                }
            }
        });
    }

    private void updateHour() {

//        List<Integer> list = new ArrayList<>();
//        for (int i = 0; i < 24; i++) {
//            list.add(i);
//        }
//        setDataList(list);

        //原始代码
        List<Integer> list = new ArrayList<>();

        List<String> listString = new ArrayList<>();

        for (int i = mMinHour; i <= mMaxHour; i++) {
            list.add(i);
            listString.add(String.format("%02d", i) + "时");
        }

        setDataList(list, listString);

    }

    public int getSelectedHour() {
        return mSelectedHour;
    }

    public void setSelectedHour(int hour) {
        setSelectedHour(hour, true);
    }

    public void setSelectedHour(int hour, boolean smootScroll) {
        setCurrentPosition(hour, smootScroll);
    }

    public void setOnHourSelectedListener(OnHourSelectedListener onHourSelectedListener) {
        mOnHourSelectedListener = onHourSelectedListener;
    }

    public interface OnHourSelectedListener {
        void onHourSelected(int hour);
    }
}
